package top.sdaily.business.controller.crm;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.sdaily.business.model.crm.Organization;
import top.sdaily.business.service.crm.OrganizationService;
import top.sdaily.core.mybatis.Page;
import top.sdaily.web.context.ReturnBody;

import java.util.List;

/**
 * Created by TsujiShiki on 2017/02/09.
 */
@RestController
@RequestMapping("organization")
public class OrganizationController {

    @Autowired
    OrganizationService organizationService;

    @RequestMapping(value = "/page/{customerId}", method = RequestMethod.GET)
    public ReturnBody getPageOrganizations(Page page, @PathVariable String customerId) {
        List<Organization> organizationList = organizationService.getPageOrganization(page, customerId);
        return ReturnBody.success().setData(organizationList).setPage(page);
    }

    @GetMapping
    public ReturnBody getOrganizations() {
        return ReturnBody.success().setData(organizationService.getAllOrganization());
    }

    @GetMapping(value = "/{id}")
    public ReturnBody getOrganizationById(@PathVariable String id) {
        return ReturnBody.success().setData(organizationService.getOrganizationById(id));
    }

    @PostMapping
    public ReturnBody addOrganization(@RequestBody Organization organization) {
        organizationService.insertOrganization(organization);
        return ReturnBody.success();
    }

    @PutMapping
    public ReturnBody updateOrganization(@RequestBody Organization organization) {
        organizationService.updateOrganization(organization);
        return ReturnBody.success();
    }
}
